#include "ros/ros.h"
#include "ros_demo/AddTwoInts.h"
#include <cstdlib>

int main(int argc, char **argv)
{
    // 初始化ROS节点
    ros::init(argc, argv, "add_two_ints_client");
    
    // 检查命令行参数
    if (argc != 3)
    {
        ROS_INFO("用法: add_two_ints_client X Y");
        return 1;
    }
    
    // 创建节点句柄
    ros::NodeHandle n;
    
    // 创建服务客户端
    ros::ServiceClient client = n.serviceClient<ros_demo::AddTwoInts>("add_two_ints");
    
    // 创建服务请求
    ros_demo::AddTwoInts srv;
    srv.request.a = atoll(argv[1]);
    srv.request.b = atoll(argv[2]);
    
    // 等待服务可用
    ROS_INFO("等待服务可用...");
    if (client.waitForExistence(ros::Duration(5.0)))
    {
        ROS_INFO("服务可用，发送请求: %ld + %ld", (long int)srv.request.a, (long int)srv.request.b);
        
        // 调用服务
        if (client.call(srv))
        {
            ROS_INFO("服务调用成功，结果: %ld", (long int)srv.response.sum);
        }
        else
        {
            ROS_ERROR("服务调用失败");
            return 1;
        }
    }
    else
    {
        ROS_ERROR("服务不可用");
        return 1;
    }
    
    return 0;
}
